package cn.richard.demo.drools.web;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.richard.demo.drools.core.DroolsReloadFlagConfigListener;
import cn.richard.demo.drools.core.KieUtilService;
import cn.richard.demo.drools.dto.Tax;

@Controller
public class IndexCtrller {
	@GetMapping({"/", "/index"})
    public String index() {
        return "index";
    }
	
	@Autowired
	private KieUtilService kieUtilService;
	
	@RequestMapping("/tax/form")
    public String tax_form(@RequestParam(required = false,defaultValue = "0") Double wage, Model model) {
		Tax tax = new Tax().setWage(wage);
		kieUtilService.fire(tax);
		model.addAttribute("tax", tax);
        return "tax-form";
    }
	@RequestMapping("/tax/form/json")
    @ResponseBody
    public Object tax_form(@RequestParam double wage) {
    	Tax tax = new Tax().setWage(wage);
    	kieUtilService.fire(tax);
        return tax;
    }
	
	@RequestMapping("/rule/reload")
	public String rule_reload() {
		kieUtilService.reload();
		return "redirect:/tax/form";
	}
	
	@Autowired
	private DroolsReloadFlagConfigListener droolsReloadFlagConfigListener;
	
	@RequestMapping("/rule/reload/nacos")
	public String rule_reload_nacos() {
		droolsReloadFlagConfigListener.publishConfig(LocalDateTime.now().toString());
		return "redirect:/tax/form";
	}
}
